package org.dtrd.modules.doctor.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.modules.doctor.entity.bean.DoctorSigninSuggestDetail;
import org.dtrd.modules.doctor.entity.bean.DoctorSigninSuggestListInfo;
import org.dtrd.modules.doctor.entity.po.DtrdEntRdDoctorSigninSuggest;
import org.dtrd.modules.doctor.entity.po.DtrdEntRdSigninSuggest;
import org.dtrd.modules.doctor.entity.request.SigninSuggestListRequest;
import org.dtrd.modules.doctor.mapper.DtrdEntRdDoctorSigninSuggestMapper;
import org.dtrd.modules.doctor.service.IDtrdEntRdDoctorSigninSuggestService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 医生端打卡建议 服务实现类
 * </p>
 *
 * @author 5146
 * @since 2022-04-08
 */
@Slf4j
@DS("multi-datasource1")
@Service
public class DtrdEntRdDoctorSigninSuggestServiceImpl extends ServiceImpl<DtrdEntRdDoctorSigninSuggestMapper, DtrdEntRdDoctorSigninSuggest> implements IDtrdEntRdDoctorSigninSuggestService {

    private DtrdEntRdDoctorSigninSuggestMapper dtrdEntRdDoctorSigninSuggestMapper;

    @Override
    public List<DoctorSigninSuggestListInfo> getDoctorSigninSuggestList(SigninSuggestListRequest request) {
//    public List<String> getDoctorSigninSuggestList(Integer signinType) {
//        List<String> list = baseMapper.listDoctorSuggestBySigninType(signinType);
//        if (list != null) {
//            return list;
////        }
//        return null;
        LambdaQueryWrapper<DtrdEntRdDoctorSigninSuggest> queryWrapper = baseQueryWrapper()
                .eq(DtrdEntRdDoctorSigninSuggest::getSigninType, request.getSigninType())
//                //暂时不提供根据医生ID查询
//                .eq(null != request.getDoctorId() && 0 != request.getDoctorId(),
//                        DtrdEntRdDoctorSigninSuggest::getDoctorId, request.getDoctorId())
                .eq(StringUtils.isNotBlank(request.getOrgCode()),
                        DtrdEntRdDoctorSigninSuggest::getOrgCode, request.getOrgCode())
                .orderByDesc(DtrdEntRdDoctorSigninSuggest::getUpdateTime);
        List<DtrdEntRdDoctorSigninSuggest> poList = list(queryWrapper);
        List<DoctorSigninSuggestListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(DoctorSigninSuggestListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public boolean saveDoctorSigninSuggest(DoctorSigninSuggestDetail dto) {
        DtrdEntRdDoctorSigninSuggest dtrdEntRdDoctorSigninSuggest = new DtrdEntRdDoctorSigninSuggest();
//        if(dto.getDataId() == null){
//            dtrdEntRdDoctorSigninSuggest = dtrdEntRdDoctorSigninSuggest.init();
//        }
        dtrdEntRdDoctorSigninSuggest.parseFromDto(dto);
        return save(dtrdEntRdDoctorSigninSuggest);
    }

    /**
     * 将患者修改的那条加入到医生的列表中去
     *
     * @param dtrdEntRdSigninSuggest 患者修改的那条信息
     * @return
     */
    @Override
    public boolean saveSigninSuggest(DtrdEntRdSigninSuggest dtrdEntRdSigninSuggest) {
        log.info("dtrdEntRdSigninSuggest====="+ dtrdEntRdSigninSuggest);
        //建立我们所需要的链表
        DtrdEntRdDoctorSigninSuggest dtrdEntRdDoctorSigninSuggest = new DtrdEntRdDoctorSigninSuggest();
        //dtrdEntRdDoctorSigninSuggest.setDoctorId(dtrdEntRdSigninSuggest.getDoctorId());
        dtrdEntRdDoctorSigninSuggest.setSigninType(dtrdEntRdSigninSuggest.getSigninType());
        dtrdEntRdDoctorSigninSuggest.setSuggestContent(dtrdEntRdSigninSuggest.getSuggestContent());
        dtrdEntRdDoctorSigninSuggest.setOrgCode(dtrdEntRdSigninSuggest.getOrgCode());
        //执行插入操作————插入到医生建议表中
        return save(dtrdEntRdDoctorSigninSuggest);
    }

    @Override
    public boolean updateSigninSuggest(Integer dataId, String signinContent) {
        //根据Id取出参数
        DtrdEntRdDoctorSigninSuggest request = getById(dataId);
        if (request != null && signinContent != null) {
            request.setSuggestContent(signinContent);
            return updateById(request);
        }else {
            return false;
        }
    }

    @Override
    public List<String> getListMouldSigninSuggest(Integer signinType) {
        List<String> list = baseMapper.listMouldSigninSuggest(signinType);
        if (list != null) {
            return list;
        }else {
            return null;
        }
    }

    @Override
    public boolean removeDoctorSigninSuggest(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdEntRdDoctorSigninSuggest> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntRdDoctorSigninSuggest.class)
                        .eq(DtrdEntRdDoctorSigninSuggest::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntRdDoctorSigninSuggest> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntRdDoctorSigninSuggest.class)
                        .eq(DtrdEntRdDoctorSigninSuggest::getIsDel, 0);
    }
}
